<?php
/*
* Copyright (c) 2010 by Cediti
*
* You may distribute under the terms of either the GNU Lesser General
* Public License or the new BSD License, as specified in the README file.
*/
// created :	26 March 2010
// version :	08/05/2010

class Cmis_Repository_Properties_Property implements Cmis_Render_Interface
{
	protected $_name;
	protected $_type;
	protected $_value;
	protected $_extras;

	public function __construct($name, $type, $value, $extras = null, $prefix = '')
	{
		$this->setName($name, $prefix);
		$this->setValue($value, $type);

		if (!empty($extras))
		{
			$this->setExtra($extras);
		}
		else
		{
			$this->_extras = array();
		}
	}

	public function getName()
	{
		return $this->_name;
	}

	public function getValue()
	{
		return $this->_value;
	}

	public function getExtras()
	{
		return $this->_extras;
	}

	/**
	 * Defined the extra properties like arguments of the xml flux
	 * @param array $extras argment => value
	 * @return Cmis_Repository_Property
	 */
	public function setExtra($extras)
	{
		$this->_extras = array();

		if (!empty($extras))
		{
			foreach($extras as $k => $v)
			{
				$this->_extras[ $k ] = (string)$v;
			}
		}

		return $this;
	}

    public function __toString()
    {
        switch($this->_type)
        {
            default :
                return (string)$this->_value;
                break;
            case Cmis_Repository_Properties::$type[ Cmis_Repository_Properties::TYPE_EXTERNAL ] :
            	return '';
            	break;
            case Cmis_Repository_Properties::$type[ Cmis_Repository_Properties::TYPE_BOOL ] :
                return $this->_value ? 'true' : 'false';
                break;
            case Cmis_Repository_Properties::$type[ Cmis_Repository_Properties::TYPE_DATE ] :
                return date_format($this->_value, DATE_ATOM);
                break;
        }
    }

	/**
	 * Define the value in function of the property type
	 * @param mixed $value
	 * @param const $type can be a constant or the type name
	 * @return Cmis_Repository_Property
	 */
	public function setValue($value, $type)
	{
		if (!is_string($type))
		{
			$type = Cmis_Repository_Properties::$type[ $type ];
		}

        $this->_type = $type;

		switch ($type)
		{
			default :
				throw new Cmis_Exception_Properties_BadType($type . ' not corresponding to an existing type');
				break;
			case Cmis_Repository_Properties::$type[ Cmis_Repository_Properties::TYPE_BOOL ] :
				$this->_value = (strcasecmp((string)$value, 'true') == 0)?true: ((strcasecmp((string)$value, 'false') == 0)?false : (bool)$value);
				break;
			case Cmis_Repository_Properties::$type[ Cmis_Repository_Properties::TYPE_DATE ] :
				$this->_value = date_create((string)$value);
				break;
			case Cmis_Repository_Properties::$type[ Cmis_Repository_Properties::TYPE_ID ] :
				$this->_value = new Cmis_Repository_Id((string)$value);
				break;
			case Cmis_Repository_Properties::$type[ Cmis_Repository_Properties::TYPE_INT ] :
				$this->_value = intval((string)$value);
				break;
            case Cmis_Repository_Properties::$type[ Cmis_Repository_Properties::TYPE_EXTERNAL ] :
                $this->_value = $value;
                break;
            case Cmis_Repository_Properties::$type[ Cmis_Repository_Properties::TYPE_STRING ] :
                $this->_value = (string)$value;
                break;
            case Cmis_Repository_Properties::$type[ Cmis_Repository_Properties::TYPE_DECIMAL ] :
				settype($value, 'float');
                $this->_value = $value;
                break;
		}

		return $this;
	}

	/**
	 * Defined a name and, if defined, cut the prefix
	 * @param string $name
	 * @param string $prefix for eg. D. from a query
	 * @return Cmis_Repository_Property
	 */
	public function setName($name, $prefix = '')
	{
		$this->_name = (!empty($prefix)) ? substr($name, strlen($prefix)) : $name;
		return $this;
	}

	public function render()
	{
		$rendered = (string)$this;

		$arguments = '';
		foreach($this->_extras as $argument => $value)
		{
			$arguments .= ' ' . $argument . '="' . $value . '"';
		}

		return <<<JCF
        <cmis:{$this->_type} localName="{$this->_name}" propertyDefinitionId="{$this->_name}"{$arguments}>
            <cmis:value>{$rendered}</cmis:value>
        </cmis:{$this->_type}>
JCF;
	}
}

# EOF
